import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {ItemDataKey} from "../../frame/dataSource/DataSource";
import {PlayerFactory} from "../../frame/PlayerFactory";

export default class SealSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.sealSkillId) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel) => {
        let success = true;
        const activePlayerId = activePlayer.playerId;
        const targetPlayerId = targetPlayer.playerId;
        if (activePlayerId === targetPlayerId) {
        } else {
            Logger.toastProduct('不能封印别人的单位！', activePlayer)
            return;
        }

        const isPet = UnitController.getInstance().isInPets(targetUnit);
        if (isPet) {
        } else {
            const nullSealItemId: string = Configer.getFinal(FinalKey.nullSealItemId) as string;
            ModelFactory.getInstance().createItemInUnit({id: nullSealItemId, lv: 0 }, activeUnit);
            Logger.toastProduct('只能封印自己的宠物', activePlayer);
            return false
        }
        const unitJson = targetUnit.getJson();

        const index = UnitController.getInstance().getPetIndex(targetUnit, activePlayer);
        UnitController.getInstance().releasePet(activePlayer, index);

        const sealItem: string = Configer.getFinal(FinalKey.sealItemId) as string;
        const item = ModelFactory.getInstance().createItemInUnit({id: sealItem, lv: 0 }, activeUnit);
        item.data[ItemDataKey.sealData] = unitJson;
        return success;
    }
}